/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Buscador;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.TreeSet;
import java.util.Vector;

/**
 *
 * @author usuario_local
 */
public class OrdenInverso {

    private HashMap<String, TreeSet<Integer> > datos;


    public OrdenInverso(){
        datos = new HashMap();
    }


    //#palabra url1 url2 url3 url4
    //#palabra
    public void addPagina(Integer url, Vector<String> palabras){
        for (int i = 0; i < palabras.size(); i++) {
            String pal = palabras.elementAt(i);
            if(datos.containsKey(pal)){
                TreeSet<Integer> arbol = datos.get(pal);
                arbol.add(url);
                datos.put(pal, arbol);
            } else{
                TreeSet<Integer> arbol = new TreeSet<Integer>();
                arbol.add(url);
                datos.put(pal, arbol);
            }

        }
    }


    public void sacaFich(File f) throws IOException{
        PrintWriter pw = new PrintWriter(new FileWriter(f));

        Iterator<Entry<String, TreeSet<Integer> > > iterHM = datos.entrySet().iterator();

        while (iterHM.hasNext()){
            Entry<String, TreeSet<Integer> > entr = iterHM.next();
            pw.print("#"+ entr.getKey());

            TreeSet<Integer> arbol = entr.getValue();
            Iterator<Integer> iTree = arbol.iterator();
            while (iTree.hasNext()){
                pw.print(" "+ (iTree.next() +1));

            }
            pw.println();
        }

        pw.flush();
        pw.close();
    }







}
